% algorithm:
% * at 3AM there is somebody => shift accordingly
% * if we find a negative value, then it means that somebody that entered has
% not been detected => increase of +"negative_value" from the last time we saw
% somebody (or, from 9.00AM if there are no '+1' before)
% * from 6PM put the occupancy to 0
%
function Fix( tFixer )
	%
	% ASSUMPTION: the times are ok
	tFixer.afFixedRealizationTimes = tFixer.afLoadedRealizationTimes;
	%
	aiIndexesOfNewDay = [1; Time.FindHour( tFixer.afLoadedRealizationTimes, 3 ) ];
	%
	for iDay = 1:numel( aiIndexesOfNewDay );
		%
		% for readability
		iFirstSample = aiIndexesOfNewDay(iDay);
		%
		if( iDay == numel( aiIndexesOfNewDay ) )
			%
			iLastSample = numel( tFixer.afLoadedRealizationTimes );
			%
		else%
			%
			iLastSample = aiIndexesOfNewDay(iDay + 1) - 1;
			%
		end;%
		%
		iOccupancyAtBeginningOfDay	= tFixer.afLoadedRealization( iFirstSample );
		iOccupancyAtEndingOfDay		= tFixer.afLoadedRealization( iLastSample );
		%
		%
		% DEBUG
% 		fprintf('day %d: beginning = %d people, ending = %d people\n', iDay, iOccupancyAtBeginningOfDay, iOccupancyAtEndingOfDay);
		%
		%
		% ----------------------------------------------------------------------
		% first step: shift the loaded realization s.t. at 3AM there is nobody
% 		fprintf('shifting everything (%d:%d) of -%d\n', iFirstSample, iLastSample, tFixer.afLoadedRealization(iFirstSample));
		tFixer.afFixedRealization(iFirstSample:iLastSample) =			...
				tFixer.afLoadedRealization(iFirstSample:iLastSample)	...
			-	tFixer.afLoadedRealization(iFirstSample);
		%
		%
		% ----------------------------------------------------------------------
		% second step: check for negative values
		%
		% initialization
		iSampleOfLastIncrement	= iFirstSample;
		iSampleOf9AM			= Time.FindHour( tFixer.afFixedRealizationTimes(iFirstSample:iLastSample), 9 ) + iFirstSample;
		%
		% 9AM might not exist in the database
		if( numel(iSampleOf9AM) == 0 )
			%
			iSampleOf9AM = -1;
			%
		end;%
		%
		for iSample = iFirstSample+1:iLastSample;
			%
			% check for increments
			if(		tFixer.afFixedRealization(iSample)		...
				>	tFixer.afFixedRealization(iSample-1)	)
				%
				iSampleOfLastIncrement = max( iSample, iSampleOf9AM );
				%
			end;%
			%
			% check for negative values
			if( tFixer.afFixedRealization(iSample) < 0 )
				%
				% in case, shift everything up from the last increment
% 				fprintf('shifting (%d:%d) of -%d\n', iSampleOfLastIncrement, iLastSample, tFixer.afFixedRealization(iFirstSample));
				tFixer.afFixedRealization(iSampleOfLastIncrement:iLastSample) =			...
						tFixer.afFixedRealization(iSampleOfLastIncrement:iLastSample)	...
					-	tFixer.afFixedRealization(iSample);
				%
			end;%
			%
		end;%
		%
		%
		% ----------------------------------------------------------------------
		% third step: from 6PM put occupancy to zero
		iSampleOf6PMMinusFirstSample = Time.FindHour( tFixer.afFixedRealizationTimes(iFirstSample:iLastSample), 18 );
		%
		% notice that that hour might not exist in the dataset (i.e., data stop
		% before 6PM)
		if( numel(iSampleOf6PMMinusFirstSample) > 0 )
			%
			iSampleOf6PM = iSampleOf6PMMinusFirstSample + iFirstSample;
			%
% 			fprintf('putting (%d:%d) to zero\n', iSampleOf6PM, iLastSample);
			tFixer.afFixedRealization(iSampleOf6PM:iLastSample) =					...
				zeros( size(tFixer.afFixedRealization(iSampleOf6PM:iLastSample))	);
			%
		end;%
		%
		%
	end;%
	%
	%
end % function
